package net.jlibrary.common.project.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Compositor;
import net.jlibrary.common.core.annotation.SearchCondition;
import net.jlibrary.common.core.annotation.SearchField;
import net.jlibrary.common.core.entity.BaseObject;

@Compositor
@Entity(name = "tab_operationlog")
@JsonIgnoreProperties(value={"operationField"})
public class OperationLog extends BaseObject {
	private static final long serialVersionUID = -4622961338811156294L;
	public static final String OPERATION_SAVE = "保存";
	public static final String OPERATION_UPDATE = "修改";
	public static final String OPERATION_DELETE = "删除";
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "account_id")
	@SearchField(value = "account", condition = SearchCondition.EQUAL)
	private Account account;// 操作人
	@Column(length = 100)
	private String comments;//对象名，从RecordOperation中定义
	@Column(length = 100)
	@SearchField(value = "operationClass", condition = SearchCondition.EQUAL)
	private String operationClass;// 操作的实体类
	@SearchField(value = "operationPk", condition = SearchCondition.EQUAL)
	private Long operationPk;// 记录主表中的pk
	@Column(length = 50)
	private String operation;// 动作
	@Column(length = 200)
	private String description;// 描述
	@OneToMany(mappedBy = "operationLog", cascade = CascadeType.ALL)
	@OrderBy("createdate desc")
	private List<OperationField> operationField = new ArrayList<OperationField>();
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT)
	@JsonFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT, timezone = CoreConstants.DATE_TIMEZONE_DEFAULT)
	private Date createdate;
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT)
	@JsonFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT, timezone = CoreConstants.DATE_TIMEZONE_DEFAULT)
	private Date lastdate;
	
	@Transient
	@SearchField(value = "createdate", condition = SearchCondition.GREATER_THAN_EQUAL)
	private Date startCreatedateSearchKey;

	public Long getId() {
		return id;
	}

	@Override
	@Transient
	public Serializable getPrimaryKey() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public String getOperationClass() {
		return operationClass;
	}

	public void setOperationClass(String operationClass) {
		this.operationClass = operationClass;
	}

	public Long getOperationPk() {
		return operationPk;
	}

	public void setOperationPk(Long operationPk) {
		this.operationPk = operationPk;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Date getLastdate() {
		return lastdate;
	}

	public void setLastdate(Date lastdate) {
		this.lastdate = lastdate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((account == null) ? 0 : account.hashCode());
		result = prime * result + ((comments == null) ? 0 : comments.hashCode());
		result = prime * result + ((createdate == null) ? 0 : createdate.hashCode());
		result = prime * result + ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((lastdate == null) ? 0 : lastdate.hashCode());
		result = prime * result + ((operation == null) ? 0 : operation.hashCode());
		result = prime * result + ((operationClass == null) ? 0 : operationClass.hashCode());
		//result = prime * result + ((operationField == null) ? 0 : operationField.hashCode());
		result = prime * result + ((operationPk == null) ? 0 : operationPk.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OperationLog other = (OperationLog) obj;
		if (account == null) {
			if (other.account != null)
				return false;
		} else if (!account.equals(other.account))
			return false;
		if (comments == null) {
			if (other.comments != null)
				return false;
		} else if (!comments.equals(other.comments))
			return false;
		if (createdate == null) {
			if (other.createdate != null)
				return false;
		} else if (!createdate.equals(other.createdate))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (lastdate == null) {
			if (other.lastdate != null)
				return false;
		} else if (!lastdate.equals(other.lastdate))
			return false;
		if (operation == null) {
			if (other.operation != null)
				return false;
		} else if (!operation.equals(other.operation))
			return false;
		if (operationClass == null) {
			if (other.operationClass != null)
				return false;
		} else if (!operationClass.equals(other.operationClass))
			return false;
//		if (operationField == null) {
//			if (other.operationField != null)
//				return false;
//		} else if (!operationField.equals(other.operationField))
//			return false;
		if (operationPk == null) {
			if (other.operationPk != null)
				return false;
		} else if (!operationPk.equals(other.operationPk))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "OperationLog [id=" + id + ", account=" + account + ", operationClass=" + operationClass
				+ ", operationPk=" + operationPk + ", operation=" + operation + ", description=" + description
				+ ", createdate=" + createdate + ", lastdate=" + lastdate + "]";
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public List<OperationField> getOperationField() {
		return operationField;
	}

	public void setOperationField(List<OperationField> operationField) {
		this.operationField = operationField;
	}

	public Date getStartCreatedateSearchKey() {
		return startCreatedateSearchKey;
	}

	public void setStartCreatedateSearchKey(Date startCreatedateSearchKey) {
		this.startCreatedateSearchKey = startCreatedateSearchKey;
	}

}
